package org.subject.entity;

/**
 * 分页对应的实体类
 * Created by viigoo_zk on 2017/4/7.
 */
public class Page {
    private int totalNumber;//总条数
    private int currentPage;//当前第几页
    private int totalPage;//总页数
    private int pageNumber = 10;//每页显示的条数
    private int dbIndex;//数据库中的 limit 的参数，从第几条开始取
    private int dbNumber;//数据库中的 limit 的参数，一共取多少条

    /**
     * 根据当前对象中属性值计算并设置相关属性值
     */
    public void count() {
        //计算总页数
        int totalPageTemp = this.totalNumber / this.pageNumber;
        int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1;
        totalPageTemp = totalPageTemp + plus;
        if (totalPageTemp <= 0) {
            totalPageTemp = 1;
        }
        //获得总页数
        this.totalPage = totalPageTemp;

        //设置当前页数
        //总页数小于当前页数，应将当前页数设置为总页数
        if (this.totalPage < this.currentPage) {
            this.currentPage = this.totalPage;
        }
        //当前页数小于1，则设置为1
        if (this.currentPage < 1) {
            this.currentPage = 1;
        }
        //设置limit的参数
        this.dbIndex = (this.currentPage - 1) * this.pageNumber;
        this.dbNumber = this.pageNumber;
    }

    public int getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(int totalNumber) {
        this.totalNumber = totalNumber;
        this.count();
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(int dbIndex) {
        this.dbIndex = dbIndex;
    }

    public int getDbNumber() {
        return dbNumber;
    }

    public void setDbNumber(int dbNumber) {
        this.dbNumber = dbNumber;
    }

    @Override
    public String toString() {
        return "Page{" +
                "totalNumber=" + totalNumber +
                ", currentPage=" + currentPage +
                ", totalPage=" + totalPage +
                ", pageNumber=" + pageNumber +
                ", dbIndex=" + dbIndex +
                ", dbNumber=" + dbNumber +
                '}';
    }
}